﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Textbook
{
    public partial class LessForm : Form
    {
        public LessForm()
        {
            InitializeComponent();
            getBooks();
        }

        List<Book> list;

        SQL sql = new SQL();

        private void getBooks()
        {
            list = sql.getBookList();
            name.Text = list[0].B_name.ToString();
            price.Text = list[0].B_price.ToString();
            name.DataSource = list;
            name.DisplayMember = "b_name";
            name.ValueMember = "b_price";
        }

        bool f = true;
        private void name_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (f)
            {
                f = false;
                return;
            }
            price.Text = name.SelectedValue.ToString();
        }

        private void submit_Click(object sender, EventArgs e)
        {
            if (num.Text == "")
            {
                MessageBox.Show("请填写数量...");
            }
            else
            {
                Less less = new Less();
                less.L_id = Utils.getTimeTicks();
                foreach (var item in list)
                {
                    if (item.B_price == decimal.Parse(price.Text) && item.B_name == name.Text)
                    {
                        less.Book_id = item.Book_id;
                        break;
                    }
                }
                less.L_time = DateTime.Parse(time.Text).ToString("yyyy-MM-dd");
                less.L_num = int.Parse(num.Text);

                if (sql.insertLess(less))
                {
                    MessageBox.Show("登记成功...");
                }
                else
                {
                    MessageBox.Show("登记失败...");
                }
            }
        }

        private void num_TextChanged(object sender, EventArgs e)
        {
            if (num.Text[num.Text.Length - 1] >= '0' && num.Text[num.Text.Length - 1] <= '9')
            {
                if (num.Text == "")
                {
                    return;
                }
                if (long.Parse(num.Text) > 10000)
                {
                    MessageBox.Show("一次最多只能订购10000本");
                    return;
                }
            }
            else
            {
                num.Text = num.Text.Remove(num.Text.Length - 1);
            }
        }
    }
}
